<?php

namespace app\admin\controller\merchant;

class UserWallet extends Base
{

    protected $model = null;
    protected $searchFields = '';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = loader('admin')->loadModel('tyyh\\UserWallet');
    }

    public function user_wallet_balance_refund()
    {
        if ($this->request->isAjax()) {
            $this->model = $model = loader('admin')->loadModel('tyyh\\UserWalletBalanceRefund');
            $this->searchFields = 'id|uid|oid|refund_out_trade_no';
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $model->alias('UWBR');
            $model->join('user_wallet_balance_order UWBO', 'UWBR.oid = UWBO.id');
            $model->append(['status_text']);
            $model->where($where);
            $model->order($sort, $order);
            $model->field('UWBR.*,UWBO.out_trade_no,money,balance');
            $list = $model->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    //拒绝退款
    public function user_wallet_balance_refund_refuse() {
        $this->model = $model = loader('admin')->loadModel('tyyh\\UserWalletBalanceRefund');
        $id = $this->request->get('id');
        $data = $model->find($id);
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $validate = loader('admin')->loadValidate('tyyh\\UserWallet');
            if(!$validate->scene('operate')->check($post)) {
                $this->error($validate->getError());
            }
            $row = loader('admin')->loadAny('only', 'tyyh\\UserWalletBalanceRefund')->get_row($post['id']);
            $row->status = loadEnum('UserWalletBalanceRefundStatus')->code2key('yijujue');
            $row->remark = $post['remark'];
            $row->save();
            $this->success('保存成功');
        }
        $this->assign('data', $data);
        return $this->view->fetch();
    }

    //同意退款
    public function user_wallet_balance_refund_approve() {
        $this->model = $model = loader('admin')->loadModel('tyyh\\UserWalletBalanceRefund');
        $id = $this->request->get('id');
        $data = $model->find($id);
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $validate = loader('admin')->loadValidate('tyyh\\UserWallet');
            if(!$validate->scene('operate')->check($post)) {
                $this->error($validate->getError());
            }
            $only = loader('admin')->loadAny('only', 'tyyh\\UserWalletBalanceRefund');
            $model = $only->getModel();

            $model->startTrans();
            $row = $only->get_row($post['id']);
            $row->status = loadEnum('UserWalletBalanceRefundStatus')->code2key('yitongguo');;
            $row->save();
            loader('admin')->loadProcess('UserWallet')->refund_ipl($post['id']);
            $model->commit();
            $this->success('保存成功');
        }
        $this->assign('data', $data);
        return $this->view->fetch();
    }
}
